import $http from '@/axios'
import {Message} from "@arco-design/web-vue";
import {useUserStore} from "@/stores/user";
import {handleSuccess,handleError} from "../common.js";

// 企业级扩展接口
export const userAPI = {
    getList: (params) => $http.get('/user/list', { params })
        .then(res => handleSuccess(res, '获取用户列表'))
        .catch(error => handleError(error, '获取用户列表', '服务器连接异常')),

    add: (data) => $http.post('/user/add', data)
        .then(res => handleSuccess(res, '添加用户'))
        .catch(error => handleError(error, '添加用户', '请检查用户信息格式')),

    delete: (userId) => $http.delete(`/user/delete/${userId}`)
        .then(res => handleSuccess(res, `删除用户[${userId}]`))
        .catch(error => handleError(error, '删除用户', '该用户可能已被删除')),

    update: (data) => $http.put(`/user/update`, data)
        .then(res => handleSuccess(res, `更新用户[${data.nickName}]`))
        .catch(error => handleError(error, '更新用户', '请检查修改内容有效性')),

    getInfo: (userId) => $http.get(`/user/info`, { params: { userId } })
        .then(res => handleSuccess(res, '获取用户信息'))
        .catch(error => handleError(error, '获取用户信息22', '用户不存在或已禁用')),

    getDept: (userId) => $http.get(`/user/dept/${userId}`)
        .then(res => handleSuccess(res, '获取用户部门'))
        .catch(error => handleError(error, '获取用户部门', '部门信息未配置')),

    getRoles: (userId) => $http.get(`/user/roles/${userId}`)
        .then(res => handleSuccess(res, '获取用户角色'))
        .catch(error => handleError(error, '获取用户角色', '角色配置异常')),

    getTokens: (userId) => $http.get(`/user/tokens/${userId}`)
        .then(res => handleSuccess(res, '获取用户令牌'))
        .catch(error => handleError(error, '获取用户令牌', '令牌信息不存在')),

    search: ({ page = 1, size = 10, keyword }) => $http.get('/user/search', {
        params: { pageNum: page, pageSize: size, keyword }
    })
        .then(res => handleSuccess(res, '用户搜索'))
        .catch(error => handleError(error, '用户搜索', '搜索条件无效'))
}

// 登录相关接口
export const authAPI = {
    getTenants: async () => $http.get('/auth/tenants')
        .then(res =>{
            return res
        })
        .catch(error => {
            Message.error('获取学校列表失败')
            return [error, null]
        }),

    login: async (data) =>{
        return $http.post('/auth/login', data)
            .then(res => {
                return res;
            })
            .catch(error => {
                const msg = error.response?.data?.msg || error.msg || '登录失败'
                Message.error(msg)
                return false;

            });

    },

    logout: () => $http.get('/auth/logout')
        .then(() => {
            Message.success('已安全退出')
            return [null, true]
        })
        .catch(error => {
            Message.error('退出登录失败')
            return [error, null]
        })
}